package com.ruoyi.project.purchase.service.impl;

import com.ruoyi.project.purchase.domain.Purch_drug;
import com.ruoyi.project.purchase.domain.Purchase;
import com.ruoyi.project.purchase.domain.Supplier;
import com.ruoyi.project.purchase.mapper.P_DrugMapper;
import com.ruoyi.project.purchase.mapper.Purch_drugMapper;
import com.ruoyi.project.purchase.mapper.PurchaseMapper;
import com.ruoyi.project.purchase.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private Purch_drugMapper purch_drugMapper;
    @Autowired
    private P_DrugMapper p_drugMapper;
    @Override
    public List<Supplier> findAllSup() {
        return purchaseMapper.findAllSup();
    }

    @Override
    public List<Purchase> findAllPur(Purchase purchase) {
        List<Purchase> allPurchase = purchaseMapper.findAllPurchase(purchase);
        return allPurchase;
    }

    @Override
    public Purchase findOnePur(String purOrderId) {
        return purchaseMapper.findOnePurchase(purOrderId);
    }

    @Override
    public Purchase returnNewPur() {
        Purchase purchase = new Purchase();
        StringBuilder stringBuilder = new StringBuilder("CG");
        for (int i = 0; i < 19; i++) {
            stringBuilder.append((int)(Math.random()*10));
        }
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        purchase.setPurOrderId(stringBuilder.toString());
        purchase.setCreateBy(name);


        return purchase;
    }

    @Override
    public int changePurVerify(String pruOrderId) {
        return purchaseMapper.changeStatus(pruOrderId);
    }

    @Override
    public int deletePurchase(String[] purOrderId) {
        int j = 0;
        for (int i = 0; i < purOrderId.length; i++) {
            j = purchaseMapper.removePur(purOrderId[i]);
            int i1 = purch_drugMapper.removePurchDrug(purOrderId[i]);
        }
        return j>0?1:0;
    }

    @Override
    public int ReChangePurVerify(String[] pruOrderIds) {
        int i = 0;
        try {
        for (String pruOrderId:pruOrderIds) {

            List<Purch_drug> purDrugByOrderId = purch_drugMapper.findPurDrugByOrderId(pruOrderId);
            System.out.println(purDrugByOrderId);
            if (purDrugByOrderId!=null) {
                for (Purch_drug purch_drug: purDrugByOrderId) {
                    int j = p_drugMapper.updateDrugNum(purch_drug);
                }
            }
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            i = purchaseMapper.ReChange(pruOrderId,name,new Date());
        }
        } catch (Exception e) {
            i=0;
        }
        return i;
    }

    @Override
    public int changeOk(String[] pruOrderIds) {
        int i = 0;
        for (String pruOrderId:pruOrderIds) {
            i += purchaseMapper.changeOk(pruOrderId);
        }
        return pruOrderIds.length==i?1:0;
    }

    @Override
    public int changeNo(String[] pruOrderIds) {
        int i = 0;
        for (String pruOrderId:pruOrderIds) {
            i += purchaseMapper.changeNo(pruOrderId);
        }
        return pruOrderIds.length==i?1:0;
    }
}
